/*
File Name          : pcase.c
*/

#include "pcase.h"

#include "beam.h"
#include "display.h"
#include "event.h"
#include "form.h"
#include "i2c.h"
#include "jtag.h"
#include "keypad.h"
#include "learning.h"
#include "prog.h"
#include "rtc.h"
#include "search.h"
#include "sensor.h"
#include "test.h"
#include "timer.h"

/*
#define	countof(a)		(sizeof(a)/sizeof(*(a)))
#define	EEPROM_WriteAddress1    0x0101
#define	EEPROM_ReadAddress1     0x0101
#define	BufferSize1             (countof(Tx1_Buffer)-1)
*/

typedef u32(*EVS)(u32);

static u16	devices[]={DEV_PANA,DEV_TCL,DEV_2,DEV_3};

u32	clock,page;
BEAM	beam,signal;
KEY	keywave[KEY_MAX];

static void Init(void){
    GPIO_InitTypeDef g;
    NVIC_InitTypeDef n;
    
    //RCC_DeInit();	//RCC reset (for debug purpose)
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    FLASH_SetLatency(FLASH_Latency_2);		//Flash 2 wait state
    RCC_HSEConfig(RCC_HSE_ON);
    if(RCC_WaitForHSEStartUp()==SUCCESS){
	RCC_HCLKConfig(RCC_SYSCLK_Div1); 	//HCLK=SYSCLK
	RCC_PCLK2Config(RCC_HCLK_Div1); 	//PCLK2=HCLK
	RCC_PCLK1Config(RCC_HCLK_Div2);		//PCLK1=HCLK/2
	RCC_PLLConfig(RCC_PLLSource_HSE_Div1,
	    RCC_PLLMul_9);			//PLLCLK=8x9=72MHz
	RCC_PLLCmd(ENABLE);
	while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
	    ;//Wait till PLL is ready
	RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
	while(RCC_GetSYSCLKSource()!=0x08)
	    ;//Wait till PLL is used as system clock source
    }
    else Fault();
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|
	RCC_APB2Periph_TIM1|RCC_APB2Periph_AFIO,ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,
	ENABLE);				//TIM2 clock enable
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,
	ENABLE);				//DMA clock enable
    
#ifdef  VECT_TAB_RAM
    NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0); 
#else  //VECT_TAB_FLASH
    NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);   
#endif
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick,0,0);
    n.NVIC_IRQChannel	=EXTI0_IRQChannel;
    n.NVIC_IRQChannelPreemptionPriority=0;
    n.NVIC_IRQChannelSubPriority=0;
    n.NVIC_IRQChannelCmd=ENABLE;
    NVIC_Init(&n);
    n.NVIC_IRQChannel	=DMA1_Channel5_IRQChannel;
    n.NVIC_IRQChannelPreemptionPriority=0;
    n.NVIC_IRQChannelSubPriority=2;
    NVIC_Init(&n);   
    
    GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,
	ENABLE);				//Use SWD
    GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1,
	ENABLE);	   			//Enable TIM1_CH2N
    g.GPIO_Pin		=GPIO_Pin_All;  
    g.GPIO_Mode		=GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA,&g);			//Keypad input
    GPIO_Write(GPIOB,0xf000);			//Turn off all LEDs
    g.GPIO_Pin		=GPIO_Pin_All-GPIO_Pin_0-GPIO_Pin_10-GPIO_Pin_11;
    g.GPIO_Mode		=GPIO_Mode_Out_PP;
    g.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,&g);			//Segments
    g.GPIO_Pin		=GPIO_Pin_0;
    g.GPIO_Mode		=GPIO_Mode_AF_PP;
    GPIO_Init(GPIOB,&g);			//Beam, TIM1_CH2N
    g.GPIO_Pin		=GPIO_Pin_1;  
    g.GPIO_Mode		=GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA,&g); 			//Prog, TIM2_CH2
    g.GPIO_Pin		=GPIO_Pin_10|GPIO_Pin_11;
    g.GPIO_Mode		=GPIO_Mode_Out_OD;
    GPIO_Init(GPIOB,&g);			//I2C
}

//------------------------------------------------------------------------------

void main(void){
    const EVS evs[]={Search,Search,Prog,Test,Learning,Learning};
    u32	evn,stt,t_i;
    
#ifdef DEBUG
    debug();
#endif
    Init();
    InitTimer();
    InitRTC();
    InitSensor();
    InitJTAG();
    
    Page(0);
    IndicatorOn();
    t_i=Timer();
    stt=STT_NIL;
    while(1){
	evn=WaitEvent();
	switch(evn&EVN_MASK){
	case EVN_KEYPRESS:
	    IndicatorOn();
	    stt=KeyPress(GetKey(evn));
	    t_i=Timer();
	    break;
	case EVN_SHAKE:
	    IndicatorOn();
	    BeamOn(KEY_CHLD);
	    BeamOff();
	    t_i=Timer();
	}
	if(TimeOut(t_i,1000)){
	    //ch_key=0;
	    t_i=0;
	    BeamOff();
	    IndicatorOff();
	}
	if(stt){
	    stt=evs[stt](stt);
	    t_i=Timer();
	}
    }
}

void Dummy(void){
}

void Fault(void){
    while(1)
	;
}

void Page(u32 pag){
    page=pag;
    Load(devices[page],keywave,64,0);
}

/*
void InitNVIC(void){
NVIC_InitTypeDef i;

i.NVIC_IRQChannel			=RTC_IRQChannel;
i.NVIC_IRQChannelPreemptionPriority	=1;
i.NVIC_IRQChannelSubPriority	=1;
NVIC_Init(&i);	//Enable the RTC NVIC interrupt
i.NVIC_IRQChannel			=DMA1_Channel7_IRQChannel;
i.NVIC_IRQChannelPreemptionPriority	=0;
i.NVIC_IRQChannelSubPriority	=2;
NVIC_Init(&i);	//Enable the DMA NVIC interrupt
i.NVIC_IRQChannel			=TIM1_UP_IRQChannel;
i.NVIC_IRQChannelPreemptionPriority	=1;
i.NVIC_IRQChannelSubPriority	=0;
i.NVIC_IRQChannelCmd		=ENABLE;
NVIC_Init(&i);	//Enable the TIM1 NVIC interrupt
i.NVIC_IRQChannel			=TIM2_IRQChannel;
i.NVIC_IRQChannelPreemptionPriority	=0;
i.NVIC_IRQChannelSubPriority	=1;
NVIC_Init(&i);	//Enable the TIM2 IR REC NVIC interrupt
}
*/
